package md.frolov.bible.initializers;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.swt.custom.StyleRange;

import md.frolov.bible.model.Archive;
import md.frolov.bible.model.Book;
import md.frolov.bible.model.Chapter;
import md.frolov.bible.model.Module;
import md.frolov.bible.model.Verse;

public class Utils {
	public static Verse getVerse(File file, String id, String lId, String chapterId, String contents, boolean replace){
		Module m = Archive.getInstance().getModuleByFile(file);
		Chapter c = (Chapter) m.getById(chapterId);
		if(replace)
			contents = contents.replaceAll("\\s*\\d+", "");
		return new Verse(id,lId,contents,c,m);
	}
	
	public static void markBooks(CharSequence cs, StyleRange sr, List<StyleRange> styles, List<Integer> ranges, List<String> references){
		List<Module> modules = new ArrayList<Module>(Archive.getInstance().getModules());
		Matcher[] ms = new Matcher[modules.size()];
		for (int i = 0; i<modules.size();i++) {
			Module module = modules.get(i);
			Pattern p = module.getStrongRegexPattern();
			ms[i] = p.matcher(cs);
		}
		
		for(int i=0;i<ms.length;i++){
			if(ms[i]==null) continue;
			
			while(ms[i].find()){
				ranges.add(ms[i].start());
				ranges.add(ms[i].end()-ms[i].start());
				styles.add(sr);
				
				Module module = modules.get(i);
				module.getUrlForStringLocation(ms[i].group());
			}
		}
	}
}
